package cn.hwl.springframework.beans.factory.support;

import cn.hwl.springframework.beans.BeansException;
import cn.hwl.springframework.beans.factory.config.BeanDefinition;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

import java.lang.reflect.Constructor;

/**
 * @author :  HuangWL
 * @date :    2022/4/24 19:33
 * Cglib 实例化----这段代码看不懂
 */
public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy {
    @Override
    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException {
        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(beanDefinition.getBeanClass());
        enhancer.setCallback(new NoOp() {
            @Override
            public int hashCode() {
                return super.hashCode();
            }
        });
        if(null == ctor) {
            return enhancer.create();
        }

        return enhancer.create(ctor.getParameterTypes(),args);

    }
}
